export default{
	state:{
		list:[
			// {
			// 	checked:false,
			// 	id: 1,
			// 	name: "但还是单身的空间卡时间段框架大大时刻到了就立刻就立刻就到了",
			// 	color: "颜色:白色且紫色",
			// 	imgUrl: "../../static/image/detail3.jpg",
			// 	pprice: 1212,
			// 	num: 121231
			// }, {
			// 	checked:false,
			// 	id: 2,
			// 	name: "但还是单身的空间卡时间段框架大大时刻到了就立刻就立刻就到了",
			// 	color: "颜色:白色且紫色",
			// 	imgUrl: "../../static/image/detail3.jpg",
			// 	pprice: 1212,
			// 	num: 121231
			// }
		],
		selectedList:[]
	},
	getters:{
		checkenAllFn(state){
			return state.list.length ===state.selectedList.length
		},
		totalCount(state){
			let total={
				pprice:0,
				num:0
			};
			state.list.forEach(v=>{
				// 是否选中
				if( state.selectedList.indexOf(v.id) > -1){
					total.pprice +=v.pprice*v.num;
					total.num =state.selectedList.length
				}
			})		
			return total;
		}
	},
	mutations:{
		// 全选
		checkAll(state){
			 state.selectedList =state.list.map(v=>{
				 v.checked =true;
				 return v.id;
			 })
		},
		// 全不选
		UncheckAll(state){
			state.list.forEach(v=>{
				v.checked =false
			})
			state.selectedList=[]
		},
		// 单选
		selectedItem(state,index){
			let id =state.list[index].id
		    let i=state.selectedList.indexOf(id)
			if( i> -1){
				state.list[index].checked =false
				return state.selectedList.splice(i,1)
			}
			
			state.list[index].checked=true
			state.selectedList.push(id);
		},
		delListFn(state){
			state.list =state.list.filter(v=>{
				return state.selectedList.indexOf(v.id) === -1;
			})
		},
		//加入购物车
		addShopCart(state,cart){
			state.list.push(cart)
		}
	},
	actions:{
		checkenAllList({commit,getters}){
			console.log(getters.checkenAllFn)
			getters.checkenAllFn ? commit("UncheckAll") : commit("checkAll");
		},
		delList({commit}){
			commit('delListFn');
			commit('UncheckAll')
			uni.showToast({
				title:"删除成功！",
				icon:'none'
			})
		}
	}
}